package com.cy.pj.service.impl;

import com.cy.pj.Dao.SysMenuDao;
import com.cy.pj.common.pojo.Node;
import com.cy.pj.common.pojo.SysMenu;
import com.cy.pj.service.SysMenuService;
import com.cy.pj.sys.commom.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
/**
 * 菜单业务对象
 */
@Transactional(readOnly = false,
              rollbackFor = Throwable.class,
                isolation = Isolation.READ_COMMITTED)
@Service
public class SysMenuServicelmpl implements SysMenuService {
    @Autowired
    private SysMenuDao sysMenuDao;

    @Transactional(readOnly = true)//查询操作是true，方法上的事务控制优先级是最高的
    @Override
    public List<SysMenu> findMenus() {
        //方式1(重点)-查找所有的菜单信息
        return sysMenuDao.selectMenus();
        //方式2(拓展)-在业务层对数据进行封装(一级菜单，二级菜单，三级菜单，...),递归算法
//List<SysMenu> menus=sysMenuDao.selectMenus();
//menus.stream().filter().collect()
    }

    @Transactional(readOnly = true)
    @Override
    public SysMenu findById(Integer id) {
        SysMenu rest=sysMenuDao.selectById(id);
        if(rest==null)
            throw new ServiceException("数据不存在");
    return rest;
    }

    @Override
    public int saveMenu(SysMenu menu) {
        return sysMenuDao.insertMenu(menu);
    }

    @Override
    public int updateMenu(SysMenu menu) {
      int rest=sysMenuDao.updateMenu(menu);
       if(rest==0)
            throw new ServiceException("您要更改的数据不存在");
   return rest;
    }

    @Transactional(readOnly = true)
    @Override
    public List<Node> findMenuTreeNodes() {
        return sysMenuDao.selectMenuTreeNode();
    }
}
